package com.ocom.accesservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.accesservice.dao.IDaoPersonnelInfo;
import com.ocom.accesservice.service.DeptInfoService;
import com.ocom.accesservice.service.MjRegionInfoService;
import com.ocom.accesservice.service.PersonnelInfoService;
import com.ocom.common.entity.access.EntityDeptInfo;
import com.ocom.common.entity.access.EntityMjRegionInfo;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.vo.PersonVo;
import com.ocom.common.vo.access.NhCountVo;
import com.ocom.common.vo.access.NhVo;
import com.ocom.common.vo.access.SchoolVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PersonnelInfoServiceImpl extends ServiceImpl<IDaoPersonnelInfo, EntityPersonnelInfo> implements PersonnelInfoService {



    //personnelInfo 持久层
    @Autowired
    private IDaoPersonnelInfo personnelInfo;


    @Autowired
    private DeptInfoService deptInfoService;

    @Autowired
    MjRegionInfoService mjRegionInfoService;


    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public EntityPersonnelInfo getPersonInfo(Long comId,Long pId) {
        return personnelInfo.selectById(pId);
    }

    //查询人员信息表personnel_info，获取单位下人员信息（comId、pId）
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public List<EntityPersonnelInfo> getPersonInfo(Long comId) {
        QueryWrapper<EntityPersonnelInfo> query = Wrappers.query();
        query.eq("com_id",comId);
        return  personnelInfo.selectList(query);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public List<PersonVo> select(Long comId, Long deptId, Integer nums) {
        return personnelInfo.selectidentity(comId,deptId,nums);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    public List<PersonVo> selectLimit(Long comId, Long deptId, Integer nums) {
        //1.查出部门列表
        List<Long> depts=new ArrayList<>();
        if(deptId>0){
            List<EntityDeptInfo> deptList=deptInfoService.getDeptList(comId,deptId);
            if(deptList!=null && deptList.size()>0){
                depts = deptList.stream().map(EntityDeptInfo::getDeptId).collect(Collectors.toList());
            }
        }
        return personnelInfo.selectLimit(comId,depts,nums);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    public NhVo getNh(Long comId) {
        List<EntityMjRegionInfo> list = mjRegionInfoService.selectMjRegionInfoList(comId,new JSONObject());

        QueryWrapper<EntityPersonnelInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        NhVo nhVo=personnelInfo.selectNh(comId);

        List<SchoolVo> list1=new ArrayList<>();
        if(list.size()>0){
            for(EntityMjRegionInfo entityMjRegionInfo:list){
                SchoolVo schoolVo =personnelInfo.selectNhPart(comId,entityMjRegionInfo.getRegionId());
                schoolVo.setRegionName(entityMjRegionInfo.getRegionName());
                schoolVo.setRegionId(entityMjRegionInfo.getRegionId());
                list1.add(schoolVo);
            }
        }
        nhVo.setList(list1);

        return nhVo;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    public NhCountVo getNhCount(Long comId) {
        return personnelInfo.nhCount(comId,7l,8l,6l);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    public Integer getFaceNumber(Long comId, Long areaId) {
        return personnelInfo.selectFaceNumber(comId,areaId);
    }


}
